Perl's Many Built-in Data Structures

Mark Leighton Fisher on 2006-12-08T17:53:11

Perl has many data structures right at hand, which is a nice change from other languages. Not only do we have arrays and hashes built-in, but we have:

  • stacks - arrays with push+pop at one end;
  • queues - arrays with push at one end and pop at the other;
  • sets - hashes where you treat the value as a Boolean;
  • bags - hashes where the value is a number;
  • doubly-linked lists - arrays with pushes and pops at either end; and
  • regexes - first-class, built-in regular expressions (I often find myself writing wrapper classes for regexes in other languages, to try and duplicate Perl's simplicity and power.)

(And that's just what I can think of off the top of my head.) No wonder I haven't found a need to build my own low-level data structures in Perl. (Perl6 looks even nicer.)

This was prompted by Searching in .Net (don't search manually, let .Net do it for you) - level 100, which presents a similar kind of functionality for .NET that we've had with grep() since at least Perl4.